require "./SysT2.css"
{Srv} = require "Srv"
{SysWin} = require "./SysWin"
{SysList} = require "./SysList"

SuperClzz = SysWin
exports["@SysT2"] = {comment:"标签页面基类"}
exports.SysT2 = new Class
  Extends: SuperClzz
  options:
    enyStr: "Emp"
    #父页面选中的行对应的实体类
    sld_eny: {}
  onDraw: ->
    t = this
    o = t.options
    elt = o.ele
    rv = yield SuperClzz.prototype.onDraw.apply t,arguments
    yield t.hInclude()
    t.initPrnWin()
    rv
  initPrnWin: ->
    t = this
    o = t.options
    elt = o.ele
    prnWin = elt.getE "[h:include]>[h:pg]"
    if prnWin
      prnWinWg = prnWin.wg()
      trSld = prnWinWg.trSld
      prnWinWg.trSld = (tr)->
        hsChg = yield trSld.apply prnWinWg,arguments
        yield t.prn_trSld prnWin,tr if hsChg
        hsChg
    return
  initSlt: (pg)->
    t = this
    o = t.options
    elt = o.ele
    win = elt.getE "[h:pg='"+pg+"']"
    winWg = win.wg()
    grid_padding = win.getE ".grid_padding"
    return if !grid_padding
    slt_but = new Element "button.slt_but[h:but=slt_but]",{title:"选择"}
    slt_but.inject grid_padding
    slt_but.addEvent "click",(e)->
      co t.slt_butClk this,e,winWg
      return
    return
  slt_butClk: (but,e,winWg)->
    t = this
    o = t.options
    elt = o.ele
    win = $ winWg
    pg = win.get "h:pg"
    body = document.body
    bodyWg = body.wg()
    menuEny = elt.retrieve "menuEny"
    opt = {elt:elt}
    opt.storeMd = {}
    opt.storeMd.initThisSrv = ->
      this.__proto__.initThisSrv.apply this,arguments
      this.options.thisSrv.options.clz = "slt_butClk."+this.options.thisSrv.options.clz
      return
    opt.storeMd.initSltTd = (tr)->
      sltTd = new Element "td.sltTd"
      sltTd.inject tr
      checkbox = new Element "input[type=checkbox].sltCbx"
      checkbox.inject sltTd
      sltTd
    winSlt = yield bodyWg.menuClkByPg but,menuEny,pg,"modal",opt
    if !winSlt
      window.ncWg.addNotice "error"," 字段 page.code 中 "+pg+" 不存在!"
      return
    winSltWg = winSlt.wg()
    grid_div = winSlt.getE ".grid_div"
    grid_tbl = grid_div.getE ".grid_tbl"
    grid_div.set "tabindex","1"
    winSlt.set "tabindex","1"
    grid_div.focus()
    sld_but_div = new Element "div",{"class":"sld_but_div",html:"""
    <button class="cancelPop">取消</button>
    <button class="confirmPop">确定</button>
    """}
    sld_but_div.inject winSlt
    grid_thead = grid_tbl.getE ".grid_thead"
    thTr = grid_thead.getLast "tr"
    sltLbl = new Element "th",{"class":"sltLbl",text:"选择"}
    sltLbl.inject thTr,"top"
    cancelPop = sld_but_div.getE ".cancelPop"
    cancelPop.addEvent "click",(e)->
      e.preventDefault() if e
      do_modal_div = winSlt.getParent ".do_modal_div"
      this.set "disabled",true
      co ->
        yield winSltWg.closePage()
        do_modal_div.destroy()
        return
      return
    confirmPop = sld_but_div.getE ".confirmPop"
    confirmPop.addEvent "click",(e)-> co ->
      e.preventDefault() if e
      confirmPop.set "disabled",true
      tbody = grid_tbl.getE "tbody"
      sltCbxArr = tbody.getEs ".sltCbx:checked"
      if sltCbxArr.length > 0
        idArr = []
        for sltCbx in sltCbxArr
          dt_tr = sltCbx.getParent ".dt_tr"
          eny = dt_tr.retrieve "eny"
          continue if !eny or !eny.id
          idArr.push eny.id
        yield o.thisSrv.ajax "slt_but"+pg,[idArr] if idArr.length > 0
      do_modal_div = winSlt.getParent ".do_modal_div"
      yield winSltWg.closePage()
      do_modal_div.destroy()
      yield winWg.dataCount()
      yield winWg.cur_pgChg true
      return
    winSlt
  initOptRmv: (pg)->
    t = this
    o = t.options
    elt = o.ele
    win = elt.getE "[h:pg='"+pg+"']"
    winWg = win.wg()
    initOptTd = winWg.initOptTd
    winWg.initOptTd = (tr)->
      optTd = initOptTd.apply this,arguments
      if !optTd
        optTd = new Element "td.optTd"
        optTd.inject tr,"top"
        optLbl = win.getE ".grid_tbl .grid_thead .optLbl"
        optLbl.show() if optLbl
      optRmv = new Element "button",{"class":"optRmv",title:"移除"}
      optRmv.addEvent "click",(e)->
        co t.optRmvClk this,e,winWg
        return
      img = new Element "div",{"class":"optRmvImg"}
      img.inject optRmv
      optRmv.inject optTd,"top"
      optTd
    return
  optRmvClk: (but,e,winWg)->
    t = this
    o = t.options
    elt = o.ele
    win = $ winWg
    pg = win.get "h:pg"
    dt_tr = but.getParent ".dt_tr"
    eny = dt_tr.retrieve "eny"
    yield o.thisSrv.ajax "optRmv"+pg,[eny.id]
    yield winWg.cur_pgChg true if winWg.cur_pgChg
    return
  "@prn_trSld":{comment:"父页面的表格选中一行"}
  prn_trSld: (prnWin,tr)->
    t = this
    o = t.options
    elt = o.ele
    return if !tr
    prnWinWg = prnWin.wg()
    prnWinOp = prnWinWg.options
    o.sld_eny = prnWinOp.sld_eny
    sld_eny = tr.retrieve "eny"
    winArr = elt.getEs "[h:include]>[h:pg]"
    for win,i in winArr
      continue if i is 0
      winWg = win.wg()
      if !tr
        yield winWg.emptyGrid()
        continue
      #给子页面强制增加筛选条件,用于跟父页面的外键关联
      yield o.thisSrv.ajax "prn_trSld",[sld_eny]
      yield winWg.dataCount()
      yield winWg.firstPgClk undefined,undefined,true
    return
  initThisSrv: ->
    t = this
    o = t.options
    elt = o.ele
    clz = "sys."+o.enyStr+"T2Srv"
    
    prnClz = ""
    prnWinArr = elt.getParents "[h:pg]"
    for i in [0...prnWinArr.length]
      prnWin = prnWinArr[prnWinArr.length-1-i]
      continue if prnWin is document.body
      prnPg = prnWin.get "h:pg"
      prnClz += prnPg+"."
    clz = prnClz+clz+"||"+clz if prnClz
    
    o.thisSrv = new Srv clz:clz
    o.thisSrv.options.ele = elt
    return
  initPg: ->
    t = this
    o = t.options
    elt = o.ele
    return
